package com.lagou.edu.ctrl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/email")
public class EmailCtrl {

  @Autowired
  private JavaMailSenderImpl javaMailSender;

  @PostMapping("/{email}/{code}")
  public boolean send(@PathVariable String email, @PathVariable String code) {
    if (StringUtils.isEmpty(email)) {
      return false;
    }
    if (StringUtils.isEmpty(code)) {
      return false;
    }
    //send email

    SimpleMailMessage message = new SimpleMailMessage();
    message.setSubject("验证码");
    message.setText(code);
    message.setTo(email);
    message.setFrom("jayqqaa12@sina.cn");
    javaMailSender.send(message);

    return true;
  }


}
